package com.a918.controller;

import com.a918.entity.Info;
import com.a918.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;
import java.util.Base64.Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
public class InfoController {
    private InfoService infoService;

    @Autowired
    public InfoController(InfoService infoService) {
        this.infoService = infoService;
    }

    @GetMapping("/import")
    public String toImportpage() {
        return "user/import";
    }

    @PostMapping("/import")
    public String importInfo(HttpServletRequest request) throws IOException {
        System.out.println(000);
//        request.setCharacterEncoding("utf=8");
        Encoder encoder = Base64.getEncoder();
        System.out.println(111);
//        String image = encoder.encodeToString(file.getBytes());
        String image = "111";
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        int age = Integer.parseInt(request.getParameter("age"));
        System.out.println(22);
        String industry = request.getParameter("industry");
        String region = request.getParameter("industry");
        String s_time = request.getParameter("s_time");
        String s_school = request.getParameter("s_school");
        String s_class = request.getParameter("s_class");
        String w_time = request.getParameter("w_time");
        String w_firm = request.getParameter("w_firm");
        String w_position = request.getParameter("w_position");
        String i_label = request.getParameter("i_label");
        int s_label = Integer.parseInt(request.getParameter("s_label"));
        System.out.println(3);
        Info user = new Info(name, sex, age, image, industry, region, s_time, s_school, s_class, w_time, w_firm, w_position, i_label, s_label);
        System.out.println(user);

        infoService.save(user);
        return "redirect:/index";
    }

}
